package com.test.tool.demo.runner.token;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 根据手机号快速获得相应环境的token
 *
 * @author 小八
 * @date 2022年01月05日10:24
 */
@Component
public class AllLoginToken extends AbstractToken {

    private String testUrl = "http://test-client.ehomepoct.com/auth/mobile/token/sms";

    private String StageUrl = "http://sta-client-api.ehomepoct.com/auth/mobile/token/sms";

    private String devLoginUrl = "http://dev-client-api.ehome.com/auth/mobile/token/sms";


    /**
     * 登入url
     */
    private String loginUrl;

    /**
     * 登入参数
     */
    private Map<String, Object> loginParams;

    /**
     * 获取测试token
     *
     * @param mobile
     * @return
     */
    public String getTestToken(String mobile) throws Exception {
        this.loginUrl = testUrl;
        this.loginParams = this.getLoginParam(mobile);
        return this.getLoginToken();
    }

    /**
     * 获取预发布token
     *
     * @param mobile
     * @return
     */
    public String getStageToken(String mobile) throws Exception {
        this.loginUrl = StageUrl;
        this.loginParams = this.getLoginParam(mobile);
        return this.getLoginToken();
    }

    /**
     * 根据参数生成登入params
     *
     * @param mobile 电话号码
     * @return map
     */
    private Map<String, Object> getLoginParam(String mobile) {
        HashMap<String, Object> formMap = new HashMap<>(4);
        formMap.put("mobile", mobile);
        formMap.put("code", initCode());
        formMap.put("grant_type", "mobile");
        formMap.put("scope", "agent");
        return formMap;
    }

    /**
     * 获取DevToken信息
     *
     * @param mobile 电话号码
     * @return 返回token
     */
    public String getDevToken(String mobile) throws Exception {
        this.loginUrl = devLoginUrl;
        this.loginParams = this.getLoginParam(mobile);
        return this.getLoginToken();
    }

    private String getLoginToken() throws Exception {
        HttpResponse authorization = HttpUtil.createPost(this.loginUrl)
            .header("authorization", getTestAuthorization())
            .form(this.loginParams)
            .execute();

        AccessTokenVO accessTokenVO = jsonToBean(authorization.body(), AccessTokenVO.class);

        // 空校验
        if (accessTokenVO == null) {
            throw new Exception("获取Token失败");
        }

        AccessTokenDetailVO data = accessTokenVO.getData();

        return data.getTokenType().concat(" ").concat(data.getAccessToken());
    }

    @Override
    public String getLoginUrl() {
        return null;
    }

    @Override
    public String getTestAuthorization() {
        return "Basic aGVhbHRoLWNsaWVudC1uZXc6aGVhbHRoLWNsaWVudC1uZXc=";
    }

    @Override
    public Map<String, Object> initLoginParam() {
        return null;
    }

    /**
     * 生成一个Code
     *
     * @return
     */
    public String initCode() {
        return DateUtil.format(new Date(), "yyMMdd");
    }

}
